﻿#region

using System;

#endregion

namespace Ymatou.CommonService
{
    /// <summary>
    ///   分页信息(1-base)
    /// </summary>
    /// <Author>binbin@CHENEY-NOTEBOOK 2010/3/27 14:47</Author>
    public class PagerInfo
    {
        #region Constructors

        private PagerInfo()
        {
        }

        /// <summary>
        ///   Initializes a new instance of the
        ///   <see cref="PagerInfo" />
        ///   class.
        /// </summary>
        /// <param name="pageIndex">当前页码(从1开始)</param>
        /// <param name="pageSize">页长</param>
        /// <param name="recordCount">记录总数</param>
        /// <Author>binbin@CHENEY-NOTEBOOK 2010/3/28 15:20</Author>
        public PagerInfo(int pageIndex, int pageSize, int recordCount)
            : this()
        {
            this.PageIndex = pageIndex;
            this.PageSize = pageSize;
            this.RecordCount = recordCount;
            this.Check();
            this.Calculate();
        }


        /// <summary>
        ///   Initializes a new instance of the
        ///   <see cref="PagerInfo" />
        ///   class.
        /// </summary>
        /// <param name="pageIndex">当前页码(从1开始)</param>
        /// <param name="pageSize">页长</param>
        /// <param name="recordCount">记录总数</param>
        /// <Author>binbin@CHENEY-NOTEBOOK 2010/3/28 15:20</Author>
        public PagerInfo(int? pageIndex, int? pageSize, int recordCount)
            : this()
        {
            if (pageIndex.HasValue)
            {
                this.PageIndex = pageIndex.Value;
            }
            if (pageSize.HasValue)
            {
                this.PageSize = pageSize.Value;
            }
            this.RecordCount = recordCount;
            this.Check();
            this.Calculate();
        }

        public PagerInfo(PagerInfo pager)
            : this(pager.PageIndex, pager.PageSize, pager.RecordCount)
        {
        }

        #endregion

        #region Pager data

        /// <summary>
        ///   Gets or sets the index of the page.(1-base)
        /// </summary>
        /// <value>The index of the page.</value>
        /// <Author>binbin@CHENEY-NOTEBOOK 2010/3/28 15:20</Author>
        public int PageIndex { get; protected set; }
        /// <summary>
        ///   Gets or sets the size of the page.
        /// </summary>
        /// <value>The size of the page.</value>
        /// <Author>binbin@CHENEY-NOTEBOOK 2010/3/28 15:20</Author>
        public int PageSize { get; protected set; }

        /// <summary>
        ///   Gets or sets the record count.
        /// </summary>
        /// <value>The record count.</value>
        /// <Author>binbin@CHENEY-NOTEBOOK 2010/3/28 15:20</Author>
        public int RecordCount { get; protected set; }

        #endregion

        #region New region

        /// <summary>
        ///   Calculates this instance.
        /// </summary>
        /// <Author>binbin@CHENEY-NOTEBOOK 2010/3/28 15:20</Author>
        protected virtual void Calculate()
        {
            int val1;
            int i = this.RecordCount % this.PageSize;
            if (i == 0)
            {
                val1 = this.RecordCount / this.PageSize;
            }
            else
            {
                val1 = this.RecordCount / this.PageSize + 1;
            }
            this.PageCount = Math.Max(val1, 1);
            if (this.PageIndex >
                this.PageCount)
            {
                this.PageIndex = this.PageCount;
            }
            
             
        }

        protected virtual void Check()
        {
            if (this.PageIndex < 1)
            {
                this.PageIndex = 1;
            }
            if (this.RecordCount < 0)
            {
                this.RecordCount = 0;
            }
            if (this.PageSize < 1)
            {
                this.PageSize = 20;
            }
        }

        #endregion

        #region Properties

        /// <summary>
        ///   是否有上一页
        /// </summary>
        /// <value>
        ///   <c>true</c>
        ///   if this instance has prev; otherwise,
        ///   <c>false</c>
        ///   .</value>
        public bool HasPrev { get { return this.PageIndex > 1; } }

        /// <summary>
        ///   是否有下一页
        /// </summary>
        /// <value>
        ///   <c>true</c>
        ///   if this instance has next; otherwise,
        ///   <c>false</c>
        ///   .</value>
        public bool HasNext { get { return this.PageIndex < this.PageCount; } }

        /// <summary>
        ///   Gets or sets the record begin.
        /// </summary>
        /// <value>The record begin.</value>
        /// <Author>binbin@CHENEY-NOTEBOOK 2010/3/28 15:20</Author>
        public int RecordBegin { get { return (this.PageIndex - 1) * this.PageSize + 1; } }

        /// <summary>
        ///   Gets or sets the record end.
        /// </summary>
        /// <value>The record end.</value>
        /// <Author>binbin@CHENEY-NOTEBOOK 2010/3/28 15:20</Author>
        public int RecordEnd { get { return this.PageIndex * this.PageSize; } }
        #region 0-base
        /// <summary>
        /// 当前页码(0-base)
        /// </summary> 
        public int PageIndexBaseZero { get { return this.PageIndex - 1; } }
        public int RecordEndBaseZero { get { return this.PageIndex * this.PageSize - 1; } }
        public int RecordBeginBaseZero { get { return (this.PageIndex - 1) * this.PageSize; } }
        #endregion
        /// <summary>
        ///   Gets or sets the page count.
        /// </summary>
        /// <value>The page count.</value>
        /// <Author>binbin@CHENEY-NOTEBOOK 2010/3/28 15:20</Author>
        public int PageCount { get; protected set; }

        /// <summary>
        ///   跳过的记录总数
        /// </summary>
        /// <value>The skip.</value>
        public int Skip
        {
            get { return (this.PageIndex - 1) * this.PageSize; }
        }

        #endregion

        public bool IsCurrentPage(int pageNumber)
        {
            return pageNumber == PageIndex;
        }

    }
}